/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.lestr.udm.core.impl.managers.filesystem;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.lestr.udm.core.api.*;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.lestr.udm.core.util.TransactionnalDataManager;

/**
 *
 * @author pibonnin
 */
public class FileSystemDataManager implements TransactionnalDataManager.IUntransactionnalDataManager {


    private File rootDirectory;


    public FileSystemDataManager() {

        rootDirectory = new File(".");

    }// END Constructor


    @Override
    public ISession openSession() {

        return new Session();

    }// END Method openSession


    public File getRootDirectory() {

        return rootDirectory;

    }// END Method getRootDirectory


    public void setRootDirectory(File rootDirectory) {

        this.rootDirectory = rootDirectory;

    }// END Method setRootDirectory


    private class Session implements TransactionnalDataManager.IUntransactionnalDataManager.ISession {


        @Override
        public void setData(boolean newData,
                            String id,
                            List<IDataType> types,
                            InputStream value) {

            try {

                if (types.get(types.size() - 1).equals(FileDataType.instance)) {

                    if (newData)
                        new File(id).createNewFile();

                    if (value != null) {

                        int nbRead;
                        byte[] buffer = new byte[1024];
                        ByteArrayOutputStream out = new ByteArrayOutputStream();

                        while ((nbRead = value.read(buffer)) > 0)
                            out.write(buffer, 0, nbRead);

                        value.close();

                    }

                } else if (types.get(types.size() - 1).equals(FolderDataType.instance)) {

                    if (newData)
                        new File(id).mkdir();
                    
                }

            } catch (IOException ex) {
                Logger.getLogger(FileSystemDataManager.class.getName()).log(Level.SEVERE, null, ex);
            }

        }// END Method setData


        @Override
        public List<IDataType> getDataTypes(String id) {

            return Arrays.asList(FileDataType.instance);

        }// END Method getDataTypes


        @Override
        public InputStream getDataValue(String id) {

            try {
                return new FileInputStream(rootDirectory + id);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(FileSystemDataManager.class.getName()).log(Level.SEVERE, null, ex);
                return null;
            }

        }// END Method getDataValue


        @Override
        public void commit() {
        }// END Method commit


        @Override
        public void close() {
        }// END Method close


    }// FIN Classe ISession


    private static class Data {


        private List<IDataType> types;


        private byte[] value;


    }// END class Data


}// END Class DataManager
